#!/usr/bin/env python 
# -*- coding:utf-8 -*-
"""
@Time:2022/2/13 3:45 下午"
@Author:lydia_liu"
@File:department.py
@function: 完成对接口的描述操作
"""

from python.API.po_api2.apis import Wework
from python.API.po_api2.testcase import Utils


class Department(Wework):


    def creart_partment(self,data):
        """创建部门"""
        url = f"https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={self.token}"
        # 企业微信中所有接口请求和响应都用json数据格式
        # r = requests.request(url=url, method='POST', json=data)
        req = {
            "url":url,
            'method':'POST',
            'json':data
        }
        r = self.send_api(req)
        return r.json()

    def update_partment(self,data):
        """更新部门"""
        url = f"https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={self.token}"
        # r = requests.request(url=url, method='POST', json=data)
        req = {
            "url": url,
            'method': 'POST',
            'json': data
        }
        r = self.send_api(req)
        return r.json()

    def delete_partment(self,id):
        """删除部门"""
        url = f"https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={self.token}&id={id}"
        # r = requests.request(url=url, method='GET')
        req = {
            "url": url,
            'method': 'GET'
        }
        r = self.send_api(req)
        return r.json()

    def select_partment(self):
        """查询所有部门信息"""
        url = f'https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={self.token}'
        # r = requests.request(url=url, method='GET')
        req = {
            "url": url,
            'method': 'GET'
        }
        r = self.send_api(req)
        return r.json()

    def clear_partment(self):
        """清理所有部门，但id=1是无法清理的"""
        depart_info = self.select_partment()
        id_list = Utils.Utils.base_jsonpath(depart_info, '$..id')
        for i in id_list:
            if i != 1:
                self.delete_partment(i)
